<!doctype html>
	<!--[if !IE]>
	<html class="no-js non-ie" lang="en-US"> <![endif]-->
	<!--[if IE 7 ]>
	<html class="no-js ie7" lang="en-US"> <![endif]-->
	<!--[if IE 8 ]>
	<html class="no-js ie8" lang="en-US"> <![endif]-->
	<!--[if IE 9 ]>
	<html class="no-js ie9" lang="en-US"> <![endif]-->
	<!--[if gt IE 9]><!-->
<html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="http://www.codediesel.com/xmlrpc.php">

<!-- favicon -->


<!--[if IE]><![endif]-->

<title>Create a quick REST API using Slim framework | codediesel</title>

<!-- All in One SEO Pack 2.2.5 by Michael Torbert of Semper Fi Web Design[-1,-1] -->
<meta name="description"  content="Create a quick REST API using Slim framework" />

<meta name="keywords"  content="rest api in using slim framework,php rest in slim framework" />

<link rel="canonical" href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/" />
<!-- /all in one seo pack -->
<link rel="alternate" type="application/rss+xml" title="codediesel &raquo; Feed" href="http://www.codediesel.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="codediesel &raquo; Comments Feed" href="http://www.codediesel.com/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="codediesel &raquo; Create a quick REST API using Slim framework Comments Feed" href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/feed/" />
<link rel='stylesheet' id='sparkling-fonts-css'  href='//fonts.googleapis.com/css?family=Open+Sans%3A400italic%2C400%2C600%2C700%7CRoboto+Slab%3A400%2C300%2C700&#038;ver=4.1.7' type='text/css' media='all' />
<link rel='stylesheet' id='genericons-css'  href='http://www.codediesel.com/wp-content/plugins/jetpack/_inc/genericons/genericons/genericons.css?ver=3.1' type='text/css' media='all' />
<link rel='stylesheet' id='mn_cache_55e4dee6bb2a3-css'  href='//www.codediesel.com/wp-content/uploads/mn_combine/6e19de6ac3330f738880e9c90e9f6f38.css?ver=4.1.7' type='text/css' media='all' />
<script type='text/javascript' src='//www.codediesel.com/wp-content/uploads/mn_combine/6bb8783e03eb0ff2e16236dc247995c2.js?ver=4.1.7'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.codediesel.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://www.codediesel.com/wp-includes/wlwmanifest.xml" /> 
<link rel='prev' title='Get the Top Seller items from Amazon using AWS' href='http://www.codediesel.com/api/getting-the-top-seller-items-from-amazon-using-aws/' />
<link rel='next' title='Finding bottlenecks in your PHP code' href='http://www.codediesel.com/testing/finding-bottlenecks-in-your-php-code/' />
<meta name="generator" content="WordPress 4.1.7" />
<link rel='shortlink' href='http://wp.me/p5WjUz-Uj' />




<!-- Meta tags added by Robots Meta: http://yoast.com/wordpress/meta-robots-wordpress-plugin/ -->
<meta name="robots" content="noarchive" />
<script type="text/javascript">
	window._wp_rp_static_base_url = 'https://wprp.zemanta.com/static/';
	window._wp_rp_wp_ajax_url = "http://www.codediesel.com/wp-admin/admin-ajax.php";
	window._wp_rp_plugin_version = '3.5.3';
	window._wp_rp_post_id = '3491';
	window._wp_rp_num_rel_posts = '6';
	window._wp_rp_thumbnails = false;
	window._wp_rp_post_title = 'Create+a+quick+REST+API+using+Slim+framework';
	window._wp_rp_post_tags = ['framework', 'slim', 'php', 'frameworks', 'databas', 'server', 'a', 'wrapper', 'write', 'php', 'student', 'slim', 'develop', 'web', 'remot', 'tabl', 'updat', 'micro', 'api'];
	window._wp_rp_promoted_content = false;
</script>
<script type="text/javascript" src="https://wprp.zemanta.com/static/js/loader.js?version=3.5.3" async></script>
<style type='text/css'>img#wpstats{display:none}</style><style type="text/css">.well .social-icons a {background-color: #dd3333 !important ;}.entry-content {font-family: Open Sans; font-size:14px; font-weight: normal; color:#565656;}.single .post-inner-content.secondary-content-box {
  display: none;
}

#secondary .widget {
 color: #6d6d6d;
}

#secondary .widget a {
    color: #6d6d6d;
}

@media (min-width: 1200px) {
    .container {
        width: 1270px;
    } 
}

</style>
</head>

<body class="single single-post postid-3491 single-format-standard">
<div id="fb-root"></div><script>(function(d, s, id) {  var js, fjs = d.getElementsByTagName(s)[0];  if (d.getElementById(id)) return;  js = d.createElement(s); js.id = id;  js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&appId=150545218452827&status=0&version=v2.0";  fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>
<div id="page" class="hfeed site">

	<header id="masthead" class="site-header" role="banner">
		<nav class="navbar navbar-default" role="navigation">
			<div class="container">
				<div class="row">
					<div class="site-navigation-inner col-sm-12">
		        <div class="navbar-header">
		            <button type="button" class="btn navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
		                <span class="sr-only">Toggle navigation</span>
		                <span class="icon-bar"></span>
		                <span class="icon-bar"></span>
		                <span class="icon-bar"></span>
		            </button>

				
				
					<div id="logo">
						<span class="site-name"><a class="navbar-brand" href="http://www.codediesel.com/" title="codediesel" rel="home">codediesel</a></span>
					</div><!-- end of #logo -->

				
		        </div>
					<div class="collapse navbar-collapse navbar-ex1-collapse"><ul id="menu-logo-bar" class="nav navbar-nav"><li id="menu-item-3512" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3512"><a title="Web Scraping eBook" href="https://leanpub.com/web-scraping">Web Scraping eBook</a></li>
<li id="menu-item-3516" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3516"><a title="WP Plugins" href="http://www.codediesel.com/plugins/">WP Plugins</a></li>
<li id="menu-item-3520" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3520"><a title="Services / Work" href="http://www.codediesel.com/services/">Services / Work</a></li>
<li id="menu-item-3521" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3521"><a title="Archives" href="http://www.codediesel.com/archives/">Archives</a></li>
<li id="menu-item-3522" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3522"><a title="About" href="http://www.codediesel.com/about/">About</a></li>
<li id="menu-item-3533" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3533"><a title="Content Index" href="http://www.codediesel.com/content-index/">Content Index</a></li>
</ul></div>					</div>
		    </div>
		  </div>
		</nav><!-- .site-navigation -->
	</header><!-- #masthead -->

	<div id="content" class="site-content">

		<div class="top-section">
								</div>

		<div class="container main-content-area">
			<div class="row">
				<div class="main-content-inner col-sm-12 col-md-8 pull-left">

	<div id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		
			
<article id="post-3491" class="post-3491 post type-post status-publish format-standard has-post-thumbnail hentry category-frameworks category-php tag-framework tag-slim">
	<img width="750" height="314" src="http://www.codediesel.com/wp-content/uploads/2015/03/slim-rest1-750x314.jpg" class="single-featured wp-post-image" alt="slim-rest" />	<div class="post-inner-content">
		<header class="entry-header page-header">

			<h1 class="entry-title ">Create a quick REST API using Slim framework</h1>

			<div class="entry-meta">
				<span class="posted-on"><i class="fa fa-calendar"></i> <a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/" rel="bookmark"><time class="entry-date published" datetime="2015-03-12T17:26:08+00:00">March 12, 2015</time><time class="updated" datetime="2015-03-21T12:19:07+00:00">March 21, 2015</time></a></span><span class="byline"> <i class="fa fa-user"></i> <span class="author vcard"><a class="url fn n" href="http://www.codediesel.com/author/admin/">sameer</a></span></span>
								<span class="cat-links"><i class="fa fa-folder-open-o"></i>
					 <a href="http://www.codediesel.com/category/frameworks/" rel="category tag">frameworks</a>, <a href="http://www.codediesel.com/category/php/" rel="category tag">php</a>				</span>
								
			</div><!-- .entry-meta -->
		</header><!-- .entry-header -->

		<div class="entry-content">
			
<!-- Quick Adsense WordPress Plugin: http://quicksense.net/ -->
<div style="float:none;margin:0px 0 0px 0;text-align:center;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inline-small-ad-square1 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-3476693800710463"
     data-ad-slot="3367592209"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<p>During a recent client project, I frequently needed to access a remote database table and update the same for certain fields. This was accomplished using phpMyAdmin on the server. However, it was getting tedious and was prone to accidental updates and deletes. Also, a couple of other developers also needed to make changes to certain fields on the remote database from their local server.</p>
<p>This is all a tedious process and prone to errors. One solution was to create a quick REST api wrapper around the remote database, using which developers could update the database table without any risk of corrupting the data and also with the added benefit of updating the table programmatically.<br />
<span id="more-3491"></span></p>
<p>This post shows how to create a simple REST API using Slim framework. Slim is a PHP micro framework which lets you write quick PHP web applications. Here we will use it to build a REST api. Although I cannot show the exact database schema used, as this would not make sense without the context, I&#8217;ve provided a simple schema for the examples. The schema shows a students table which provides student scores along with their names.</p>

<div class="wp_codebox"><table><tr id="p34911"><td class="code" id="p3491code1"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`students`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`student_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">auto_increment</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`score`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`first_name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`last_name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">default</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`student_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;</pre></td></tr></table></div>

<h4>Installation</h4>
<p>You can install Slim framework using Composer or with a manual install. I prefer using Composer. If you will be using Slim in a sub-directory than make sure you use a .htaccess as given below.</p>

<div class="wp_codebox"><table><tr id="p34912"><td class="code" id="p3491code2"><pre class="text" style="font-family:monospace;">RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]</pre></td></tr></table></div>

<h4>Designing our REST api</h4>
<p>Before we design the actual code we need to construct the REST endpoints we will be needing to accomplish our goals. In our small example here we will only be needing to get the student score or update the score. Say our url is &#8216;http://somedomain.com&#8217; and Slim is installed in the &#8216;api&#8217; directory. We will be needing 3 endpoints:</p>

<div class="wp_codebox"><table><tr id="p34913"><td class="code" id="p3491code3"><pre class="text" style="font-family:monospace;">http://somedomain.com/api/                   #Home endpoint
http://somedomain.com/api/getScore           #Get student score
http://somedomain.com/api/updateScore        #Update student score</pre></td></tr></table></div>

<p>The simplest thing you can do with Slim is return a welcome string for the home route. This is the simplest Slim application you can design. The url for our home route will be &#8216;http://somedomain.com/api/&#8217;. This just returns the string &#8216;Welcome to Slim based API&#8217;.</p>

<div class="wp_codebox"><table><tr id="p34914"><td class="code" id="p3491code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require</span> <span style="color: #0000ff;">'vendor/autoload.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Slim\Slim<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Welcome to Slim 3.0 based API&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Note the use of the <code>use</code> operator to import the <code>$app</code> object in the current anonymous function context. You can also do it differently, by getting an instance of the $app object in the function.</p>

<div class="wp_codebox"><table><tr id="p34915"><td class="code" id="p3491code5"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> \Slim\Slim<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Welcome to Slim 3.0 based API&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The next endpoint will be to get a student score, this requires that we pass a student id in the url. For example:</p>
<!-- Quick Adsense WordPress Plugin: http://quicksense.net/ -->
<div style="float:none;margin:10px 0 10px 0;text-align:center;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inline-large-ad-square-2 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-3476693800710463"
     data-ad-slot="1458710204"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>


<div class="wp_codebox"><table><tr id="p34916"><td class="code" id="p3491code6"><pre class="text" style="font-family:monospace;"># Get score for student having the id of 3
http://somedomain.com/api/getScore/3</pre></td></tr></table></div>

<p>The code for the same is shown below.</p>

<div class="wp_codebox"><table><tr id="p34917"><td class="code" id="p3491code7"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/getScore/:id'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> \Slim\Slim<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    try 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> getDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * 
            FROM students
            WHERE student_id = :id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$student</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_OBJ</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$student</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$student</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PDOException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No records found.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'{&quot;error&quot;:{&quot;text&quot;:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'}}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This returns the score of a student when we pass a appropriate student id. Once we find the student score we return the complete student information as a JSON string with the appropriate &#8216;content-type&#8217; and &#8216;status&#8217; headers. For any error we throw an exception and return a &#8216;404&#8217; status.</p>
<p>Now for the &#8216;updateScore&#8217; endpoint. The code is given below.</p>

<div class="wp_codebox"><table><tr id="p34918"><td class="code" id="p3491code8"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/updateScore'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> \Slim\Slim<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$allPostVars</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$score</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allPostVars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'score'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allPostVars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    try 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> getDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE students 
            SET score = :score 
            WHERE student_id = :id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':score'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$score</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;code&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'{&quot;error&quot;:{&quot;text&quot;:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'}}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As this is a POST request, we gather all the parameters in the $allPostVars variable using the &#8216;$app->request->post()&#8217; method. As in the previous function we return a JSON success string on a valid update or throw an error.</p>
<p>The complete Slim REST API is shown below. The getDB() function is a db helper which provides a db instance for each route. Note that this is not the best way to code a db access function, I&#8217;ve used it just for illustration. In a production application I would use a singleton pattern to get the db object each time.</p>

<div class="wp_codebox"><table><tr id="p34919"><td class="code" id="p3491code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require</span> <span style="color: #0000ff;">'vendor/autoload.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dbhost</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dbuser</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dbpass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dbname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mysql_conn_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mysql:host=<span style="color: #006699; font-weight: bold;">$dbhost</span>;dbname=<span style="color: #006699; font-weight: bold;">$dbname</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dbConnection</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mysql_conn_string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$dbConnection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_ERRMODE</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">ERRMODE_EXCEPTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$dbConnection</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Slim\Slim<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Welcome to Slim based API&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/getScore/:id'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> \Slim\Slim<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    try 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> getDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * 
            FROM students
            WHERE student_id = :id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$student</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_OBJ</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$student</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$student</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PDOException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No records found.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'{&quot;error&quot;:{&quot;text&quot;:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'}}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/updateScore'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$allPostVars</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$score</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allPostVars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'score'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allPostVars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    try 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> getDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE students 
            SET score = :score 
            WHERE student_id = :id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':score'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$score</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;code&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'{&quot;error&quot;:{&quot;text&quot;:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'}}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Testing our REST api</h4>
<p>Testing the api is simple, just enter the endpoints in the url and see the response, or better yet you can use curl. For example you can test the home route with curl as given below,  which also shows the output.</p>

<div class="wp_codebox"><table><tr id="p349110"><td class="code" id="p3491code10"><pre class="text" style="font-family:monospace;">C:\localhost\test&gt;curl -i http://localhost/projects/api/
&nbsp;
HTTP/1.1 200 OK
Date: Fri, 13 Mar 2015 05:22:33 GMT
Server: Apache/2.2.11 (Win32) PHP/5.4.31
X-Powered-By: PHP/5.4.31
Content-Length: 25
Content-Type: text/html
&nbsp;
Welcome to Slim based API</pre></td></tr></table></div>

<p>The same with the second route, notice the headers and status values. Also note the last response JSON string.</p>

<div class="wp_codebox"><table><tr id="p349111"><td class="code" id="p3491code11"><pre class="text" style="font-family:monospace;">C:\localhost\test&gt;curl -i http://localhost/projects/api/getScore/3
&nbsp;
HTTP/1.1 200 OK
Date: Fri, 13 Mar 2015 05:24:56 GMT
Server: Apache/2.2.11 (Win32) PHP/5.4.31
X-Powered-By: PHP/5.4.31
Content-Length: 71
Content-Type: application/json
&nbsp;
{&quot;student_id&quot;:&quot;3&quot;,&quot;score&quot;:&quot;22&quot;,&quot;first_name&quot;:&quot;Ethan&quot;,&quot;last_name&quot;:&quot;Yang&quot;}</pre></td></tr></table></div>

<p>The third endpoint requires that you post the student id and the new score that you want to update as a POST request. With CURL you can do as given below.</p>

<div class="wp_codebox"><table><tr id="p349112"><td class="code" id="p3491code12"><pre class="text" style="font-family:monospace;">C:\localhost\test&gt;curl -i --data &quot;id=3&amp;score=36&quot;  http://localhost/projects/api/updateScore
&nbsp;
HTTP/1.1 200 OK
Date: Fri, 13 Mar 2015 05:27:50 GMT
Server: Apache/2.2.11 (Win32) PHP/5.4.31
X-Powered-By: PHP/5.4.31
Content-Length: 29
Content-Type: application/json
&nbsp;
{&quot;status&quot;:&quot;success&quot;,&quot;code&quot;:1}</pre></td></tr></table></div>

<p>Although CURL is quick and handy, my favorite tool to test REST apis is a Chrome extension named <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en" title="postman rest client">Postman</a>, it is a extremely handy REST client especially when it comes to testing POST requests.</p>
<h4>In conclusion</h4>
<p>Slim framework offers a clean and quick way to build a REST api. Although it does not have built-in libraries for authentication you can easily add 3rd party code for the same. The extremely small size of the base framework allows a quick installation on any server. In future posts we will look into some authentication methods for Slim framework.</p>

<div style="font-size:0px;height:0px;line-height:0px;margin:0;padding:0;clear:both"></div><div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-official sd-sharing"><h3 class="sd-title">Share this:</h3><div class="sd-content"><ul><li class="share-facebook"><div class="like_button"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.codediesel.com%2Fphp%2Fcreate-a-quick-rest-api-using-slim-framework%2F&amp;layout=button_count&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=21&amp;locale=en_US&amp;width=90" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:96px; height:21px;" allowTransparency="true"></iframe></div></li><li class="share-twitter"><div class="twitter_button"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwww.codediesel.com%2Fphp%2Fcreate-a-quick-rest-api-using-slim-framework%2F&#038;counturl=http%3A%2F%2Fwww.codediesel.com%2Fphp%2Fcreate-a-quick-rest-api-using-slim-framework%2F&#038;count=horizontal&#038;text=Create%20a%20quick%20REST%20API%20using%20Slim%20framework%3A&#038;via=codediesel" style="width:101px; height:20px;"></iframe></div></li><li class="share-google-plus-1"><div class="googleplus1_button"><div class="g-plus" data-action="share" data-annotation="bubble" data-href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/"></div></div></li><li class="share-stumbleupon"><div class="stumbleupon_button"><iframe src="http://www.stumbleupon.com/badge/embed/1/?url=http%3A%2F%2Fwww.codediesel.com%2Fphp%2Fcreate-a-quick-rest-api-using-slim-framework%2F&amp;title=Create%20a%20quick%20REST%20API%20using%20Slim%20framework" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px;" allowTransparency="true"></iframe></div></li><li class="share-linkedin"><div class="linkedin_button"><script type="in/share" data-url="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/" data-counter="right"></script></div></li><li class="share-end"></li></ul></div></div></div>            	      <!-- tags -->
	      <div class="tagcloud">

	          <a href="http://www.codediesel.com/tag/framework/">framework</a> <a href="http://www.codediesel.com/tag/slim/">slim</a> 
	      </div>
	      <!-- end tags -->
	                              <hr/>
            
            
<div class="wp_rp_wrap  wp_rp_plain" id="wp_rp_first"><div class="wp_rp_content"><ul class="related_post wp_rp"><li data-position="0" data-poid="in-3184" data-post-type="none" ><a href="http://www.codediesel.com/javascript/6-essential-javascript-frameworks-for-building-web-apps/" class="wp_rp_title">6 Essential JavaScript frameworks for building web apps</a></li><li data-position="1" data-poid="in-3228" data-post-type="none" ><a href="http://www.codediesel.com/frameworks/simple-user-authentication-in-laravel-4/" class="wp_rp_title">Simple user authentication in Laravel 4</a></li><li data-position="2" data-poid="in-2912" data-post-type="none" ><a href="http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/" class="wp_rp_title">Automatically create PHP classes from MySQL</a></li><li data-position="3" data-poid="in-103" data-post-type="none" ><a href="http://www.codediesel.com/php/ping-a-server-using-php/" class="wp_rp_title">Ping a server using PHP</a></li><li data-position="4" data-poid="in-3437" data-post-type="none" ><a href="http://www.codediesel.com/design/selecting-a-responsive-css-framework/" class="wp_rp_title">Selecting a responsive CSS framework</a></li><li data-position="5" data-poid="in-123" data-post-type="none" ><a href="http://www.codediesel.com/php/sidu-a-free-mysql-client/" class="wp_rp_title">Sidu &#8211; a Free MySQL client</a></li></ul></div></div>
            
            <hr/>
			
		</div><!-- .entry-content -->

		<footer class="entry-meta">

	    	

		</footer><!-- .entry-meta -->
        
	</div>
				<div class="post-inner-content secondary-content-box">
	        <!-- author bio -->
	        <div class="author-bio content-box-inner">

	          <!-- avatar -->
	          <div class="avatar">
	              <img alt='' src='http://1.gravatar.com/avatar/fa9df688ce1e6ff39052faa763e839ff?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />	          </div>
	          <!-- end avatar -->

	          <!-- user bio -->
	          <div class="author-bio-content">

	            <h4 class="author-name"><a href="http://www.codediesel.com/author/admin/">sameer</a></h4>
	            <p class="author-description">
	                	            </p>

	          </div>
	          <!-- end author bio -->

	        </div>
	        <!-- end author bio -->
			</div>
            </article><!-- #post-## -->



			
<div id="comments" class="comments-area hidden-print">

	
			<h2 class="comments-title">
			10 thoughts on &ldquo;<span>Create a quick REST API using Slim framework</span>&rdquo;		</h2>

		
		<ol class="comment-list">
					<li id="comment-115534" class="comment even thread-even depth-1">
			<article id="div-comment-115534" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/718cd9da75b0633a16ad437d389e90b9?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn">l80</b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-115534">
							<time datetime="2015-03-19T20:12:47+00:00">
								March 19, 2015 at 8:12 pm							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>Every newby will use your example and put Model to the Front Controller.</p>
<p>Thank you!</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-115630" class="comment byuser comment-author-admin bypostauthor odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-115630" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/fa9df688ce1e6ff39052faa763e839ff?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://www.codediesel.com' rel='external nofollow' class='url'>sameer</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-115630">
							<time datetime="2015-03-20T08:35:56+00:00">
								March 20, 2015 at 8:35 am							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>Slim is a &#8216;micro&#8217; framework and does not have MVC style builtin, need to add libraries like <a href="https://github.com/revuls/SlimMVC" rel="nofollow">https://github.com/revuls/SlimMVC</a>. The example was to show how to develop a quick REST API for internal use and not for production.</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-120800" class="comment even thread-even depth-1">
			<article id="div-comment-120800" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://0.gravatar.com/avatar/ed93c06c209f6f77ca2be58a510ba75d?s=60&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://cvk.biz/alurent71316' rel='external nofollow' class='url'>Alurent Cream</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-120800">
							<time datetime="2015-04-16T07:31:06+00:00">
								April 16, 2015 at 7:31 am							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>Thanks for finally talking about &gt; Create a quick REST API using Slim<br />
framework | codediesel &lt; Liked it!</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-130752" class="comment odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-130752" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/35059a88c102ef132a1bb1f3ef535e21?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn">Ismail</b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-130752">
							<time datetime="2015-06-04T15:53:54+00:00">
								June 4, 2015 at 3:53 pm							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>Don&#8217;t you think an update should be done with PUT and not POST ? Post is to create new student record</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-138432" class="comment even thread-even depth-1">
			<article id="div-comment-138432" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/7922a5292cbb907e5859212aa58ce122?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://giosyst3m.net' rel='external nofollow' class='url'>Giosyst3m</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-138432">
							<time datetime="2015-07-20T00:06:42+00:00">
								July 20, 2015 at 12:06 am							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>Thank you, very easy to understand !!!!</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-139236" class="comment odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-139236" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/b70185c5314ce081df8cf977ce968315?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn">John</b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-139236">
							<time datetime="2015-07-24T13:21:59+00:00">
								July 24, 2015 at 1:21 pm							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>What is the &#8216;vendor/autoload.php'; file? How did you get it?<br />
the slim framework has no autload file</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-139240" class="comment byuser comment-author-admin bypostauthor even thread-even depth-1">
			<article id="div-comment-139240" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/fa9df688ce1e6ff39052faa763e839ff?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://www.codediesel.com' rel='external nofollow' class='url'>sameer</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-139240">
							<time datetime="2015-07-24T13:27:49+00:00">
								July 24, 2015 at 1:27 pm							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>When you install Slim via Composer, Composer will create a autoload file in the vendor folder.</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-139241" class="comment byuser comment-author-admin bypostauthor odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-139241" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/fa9df688ce1e6ff39052faa763e839ff?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://www.codediesel.com' rel='external nofollow' class='url'>sameer</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-139241">
							<time datetime="2015-07-24T13:49:31+00:00">
								July 24, 2015 at 1:49 pm							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>I&#8217;ll be writing a post on how to manually install slim framework in a few hours.</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-141759" class="comment even thread-even depth-1">
			<article id="div-comment-141759" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/7bf874cc1b12f63ba1a56fd0903549ba?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn">Jason</b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-141759">
							<time datetime="2015-08-07T10:01:23+00:00">
								August 7, 2015 at 10:01 am							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>I am getting an Getting notice Undefined index when trying to use updateScore method, get works just fine.</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		<li id="comment-141776" class="comment byuser comment-author-admin bypostauthor odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-141776" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<img alt='' src='http://1.gravatar.com/avatar/fa9df688ce1e6ff39052faa763e839ff?s=60&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />						<b class="fn"><a href='http://www.codediesel.com' rel='external nofollow' class='url'>sameer</a></b> <span class="says">says:</span>					</div><!-- .comment-author -->

					<div class="comment-metadata">
						<a href="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/comment-page-1/#comment-141776">
							<time datetime="2015-08-07T11:30:27+00:00">
								August 7, 2015 at 11:30 am							</time>
						</a>
											</div><!-- .comment-metadata -->

									</footer><!-- .comment-meta -->

				<div class="comment-content">
					<p>&#8216;updateScore&#8217; is a POST method, make sure you are passing all parameters via a POST request.</p>
				</div><!-- .comment-content -->

							</article><!-- .comment-body -->
</li><!-- #comment-## -->
		</ol><!-- .comment-list -->

		
	
	
									<div id="respond" class="comment-respond">
				<h3 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/php/create-a-quick-rest-api-using-slim-framework/#respond" style="display:none;">Cancel reply</a></small></h3>
									<form action="http://www.codediesel.com/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate>
																			<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>							<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" aria-required='true' /></p>
<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" aria-describedby="email-notes" aria-required='true' /></p>
<p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="url" value="" size="30" /></p>
<p>
<label for="mcspvalue">Spam protection: Sum of 3 + 6 ?</label> <span class="required">*</span>
<input id="mcspvalue" name="mcspvalue" type="text" value="" size="30" aria-required="true" />
<input type="hidden" name="mcspinfo" value="05bac" />
</p>												<p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-describedby="form-allowed-tags" aria-required="true"></textarea></p>						<p class="form-allowed-tags" id="form-allowed-tags">You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:  <code>&lt;a href=&quot;&quot; title=&quot;&quot;&gt; &lt;abbr title=&quot;&quot;&gt; &lt;acronym title=&quot;&quot;&gt; &lt;b&gt; &lt;blockquote cite=&quot;&quot;&gt; &lt;cite&gt; &lt;code&gt; &lt;del datetime=&quot;&quot;&gt; &lt;em&gt; &lt;i&gt; &lt;q cite=&quot;&quot;&gt; &lt;strike&gt; &lt;strong&gt; </code></p>						<p class="form-submit">
							<input name="submit" type="submit" id="submit" class="submit" value="Post Comment" />
							<input type='hidden' name='comment_post_ID' value='3491' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
						</p>
						<p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="3843d85503" /></p><p class="comment-subscription-form"><input type="checkbox" name="subscribe_comments" id="subscribe_comments" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;" /> <label class="subscribe-label" id="subscribe-label" for="subscribe_comments">Notify me of follow-up comments by email.</label></p><p class="comment-subscription-form"><input type="checkbox" name="subscribe_blog" id="subscribe_blog" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;" /> <label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">Notify me of new posts by email.</label></p><p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="198"/></p>					</form>
							</div><!-- #respond -->
			
</div><!-- #comments -->

				<nav class="navigation post-navigation" role="navigation">
		<h1 class="screen-reader-text">Post navigation</h1>
		<div class="nav-links">
			<div class="nav-previous"><a href="http://www.codediesel.com/api/getting-the-top-seller-items-from-amazon-using-aws/" rel="prev"><i class="fa fa-chevron-left"></i> Get the Top Seller items from Amazon using AWS</a></div><div class="nav-next"><a href="http://www.codediesel.com/testing/finding-bottlenecks-in-your-php-code/" rel="next">Finding bottlenecks in your PHP code <i class="fa fa-chevron-right"></i></a></div>		</div><!-- .nav-links -->
	</nav><!-- .navigation -->
	
		
		</main><!-- #main -->
	</div><!-- #primary -->

</div>
	<div id="secondary" class="widget-area col-sm-12 col-md-4 hidden-print" role="complementary">
		<div class="well">
						<aside id="sparkling-social-2" class="widget sparkling-social"><h3 class="widget-title">Follow codediesel</h3>
    <!-- social icons -->
    <div class="social-icons sticky-sidebar-social">


    <div class="social-icons"><a href="https://www.facebook.com/codediesel" title="Follow us on Facebook" class="facebook" target="_blank"><i class="social_icon fa fa-facebook"></i></a><a href="https://twitter.com/codediesel" title="Follow us on Twitter" class="twitter" target="_blank"><i class="social_icon fa fa-twitter"></i></a><a href="https://plus.google.com/+SameerBorate" title="Follow us on Google+" class="googleplus" target="_blank"><i class="social_icon fa fa-googleplus"></i></a><a href="http://feeds.feedburner.com/Code-diesel" title="Follow us on RSS" class="rss" target="_blank"><i class="social_icon fa fa-rss"></i></a><a href="https://github.com/codediesel" title="Follow us on GitHub" class="github" target="_blank"><i class="social_icon fa fa-github"></i></a></div>

    </div><!-- end social icons -->


		</aside><aside id="blog_subscription-2" class="widget jetpack_subscription_widget"><h3 class="widget-title">Subscribe to Blog via Email</h3>
			<form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-blog_subscription-2">
				<div id="subscribe-text"><p>Enter your email address to subscribe to this blog and receive notifications of new posts by email.</p>
</div>					<p id="subscribe-email">
						<label id="jetpack-subscribe-label" for="subscribe-field">
							Email Address						</label>
						<input type="email" name="email" value="" id="subscribe-field" placeholder="Email Address" />
					</p>

					<p id="subscribe-submit">
						<input type="hidden" name="action" value="subscribe" />
						<input type="hidden" name="source" value="http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/" />
						<input type="hidden" name="sub-type" value="widget" />
						<input type="hidden" name="redirect_fragment" value="blog_subscription-2" />
												<input type="submit" value="Subscribe" name="jetpack_subscriptions_widget" />
					</p>
							</form>

			<script>
				( function( d ) {
					if ( ( 'placeholder' in d.createElement( 'input' ) ) ) {
						var label = d.getElementById( 'jetpack-subscribe-label' );
	 					label.style.clip 	 = 'rect(1px, 1px, 1px, 1px)';
	 					label.style.position = 'absolute';
	 					label.style.height   = '1px';
	 					label.style.width    = '1px';
	 					label.style.overflow = 'hidden';
					}
				} ) ( document );
			</script>
		 
		
</aside><aside id="text-3" class="widget widget_text">			<div class="textwidget"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- imagead-sidebar -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-3476693800710463"
     data-ad-slot="4009766543"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
		</aside>		<aside id="recent-posts-4" class="widget widget_recent_entries">		<h3 class="widget-title">Recent Posts</h3>		<ul>
					<li>
				<a href="http://www.codediesel.com/nodejs/how-to-create-a-mock-rest-api-for-testing-in-nodejs/">How to create a mock REST api for testing in nodejs</a>
						</li>
					<li>
				<a href="http://www.codediesel.com/php/how-to-manually-install-slim-framework/">How to manually install Slim framework</a>
						</li>
					<li>
				<a href="http://www.codediesel.com/php/cron-expression-parser-in-php/">Cron Expression Parser in PHP</a>
						</li>
					<li>
				<a href="http://www.codediesel.com/mobile/building-a-cordova-mobile-app-with-netbeans/">Building a Cordova mobile app with NetBeans</a>
						</li>
					<li>
				<a href="http://www.codediesel.com/wordpress/accessing-wordpress-data-using-the-new-rest-api/">Accessing WordPress data using the new REST api</a>
						</li>
					<li>
				<a href="http://www.codediesel.com/php/php-5-5-on-oracle-virtualbox/">PHP 5.5 on Oracle VirtualBox</a>
						</li>
				</ul>
		</aside><aside id="text-4" class="widget widget_text">			<div class="textwidget"><a href="https://leanpub.com/web-scraping"><img width="300px;" src="http://www.codediesel.com/wp-content/themes/compositio/images/web-scraping-ebook.jpg" /></a></div>
		</aside><aside id="facebook-likebox-2" class="widget widget_facebook_likebox"><h3 class="widget-title"><a href="https://www.facebook.com/codediesel">Facebook</a></h3><iframe src="https://www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fcodediesel&#038;width=300&#038;height=250&#038;colorscheme=light&#038;show_faces=true&#038;stream=false&#038;show_border=true&#038;header=false&#038;force_wall=false" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: 300px;  height: 250px; background: #fff"></iframe></aside>		</div>
	</div><!-- #secondary -->
			</div><!-- close .*-inner (main-content or sidebar, depending if sidebar is used) -->
		</div><!-- close .row -->
	</div><!-- close .container -->
</div><!-- close .site-content -->

	<div id="footer-area hidden-print">
		<div class="container footer-inner">
			<div class="row">
				
				</div>
		</div>

		<footer id="colophon" class="site-footer hidden-print" role="contentinfo">
			<div class="site-info container">
				<div class="row">
					<div class="social-icons"><a href="https://www.facebook.com/codediesel" title="Follow us on Facebook" class="facebook" target="_blank"><i class="social_icon fa fa-facebook"></i></a><a href="https://twitter.com/codediesel" title="Follow us on Twitter" class="twitter" target="_blank"><i class="social_icon fa fa-twitter"></i></a><a href="https://plus.google.com/+SameerBorate" title="Follow us on Google+" class="googleplus" target="_blank"><i class="social_icon fa fa-googleplus"></i></a><a href="http://feeds.feedburner.com/Code-diesel" title="Follow us on RSS" class="rss" target="_blank"><i class="social_icon fa fa-rss"></i></a><a href="https://github.com/codediesel" title="Follow us on GitHub" class="github" target="_blank"><i class="social_icon fa fa-github"></i></a></div>					<nav role="navigation" class="col-md-6">
											</nav>
					<div class="copyright col-md-6">
						<a href="http://www.codediesel.com/" title="code-diesel">code-diesel</a>  All rights reserved.						Theme by <a href="http://colorlib.com/" target="_blank">Colorlib</a> Powered by <a href="http://wordpress.org/" target="_blank">WordPress</a>					</div>
				</div>
			</div><!-- .site-info -->
			<div class="scroll-to-top"><i class="fa fa-angle-up"></i></div><!-- .scroll-to-top -->
		</footer><!-- #colophon -->
	</div>
</div><!-- #page -->

	<div style="display:none">
	<div class="grofile-hash-map-fa9df688ce1e6ff39052faa763e839ff">
	</div>
	<div class="grofile-hash-map-718cd9da75b0633a16ad437d389e90b9">
	</div>
	<div class="grofile-hash-map-ed93c06c209f6f77ca2be58a510ba75d">
	</div>
	<div class="grofile-hash-map-35059a88c102ef132a1bb1f3ef535e21">
	</div>
	<div class="grofile-hash-map-7922a5292cbb907e5859212aa58ce122">
	</div>
	<div class="grofile-hash-map-b70185c5314ce081df8cf977ce968315">
	</div>
	<div class="grofile-hash-map-7bf874cc1b12f63ba1a56fd0903549ba">
	</div>
	</div>

	<script type="text/javascript">
		WPCOM_sharing_counts = {"http:\/\/www.codediesel.com\/php\/create-a-quick-rest-api-using-slim-framework\/":3491}	</script>
		<script type="text/javascript">
			var windowOpen;
		jQuery(document).on( 'ready post-load', function(){
			jQuery( 'a.share-facebook' ).on( 'click', function() {
				if ( 'undefined' !== typeof windowOpen ){ // If there's another sharing window open, close it.
					windowOpen.close();
				}
				windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomfacebook', 'menubar=1,resizable=1,width=600,height=400' );
				return false;
			});
		});
		</script>
				<script type="text/javascript">
			var windowOpen;
		jQuery(document).on( 'ready post-load', function(){
			jQuery( 'a.share-twitter' ).on( 'click', function() {
				if ( 'undefined' !== typeof windowOpen ){ // If there's another sharing window open, close it.
					windowOpen.close();
				}
				windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomtwitter', 'menubar=1,resizable=1,width=600,height=350' );
				return false;
			});
		});
		</script>
					<script type="text/javascript">
			  (function() {
			    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			    po.src = 'https://apis.google.com/js/plusone.js';
			    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
			  })();
			</script>
			<script type="text/javascript">
			jQuery( document ).ready( function() {
			    jQuery.getScript( '//platform.linkedin.com/in.js?async=true', function success() {
			        IN.init();
			    });
			});
			jQuery( document.body ).on( 'post-load', function() {
				if ( typeof IN != 'undefined' )
					IN.parse();
			});
			</script><script type='text/javascript' src='http://s0.wp.com/wp-content/js/devicepx-jetpack.js?ver=201536'></script>
<script type='text/javascript' src='http://s.gravatar.com/js/gprofiles.js?ver=2015Augaa'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var WPGroHo = {"my_hash":""};
/* ]]> */
</script>
<script type='text/javascript' src='http://www.codediesel.com/wp-content/plugins/jetpack/modules/wpgroho.js?ver=4.1.7'></script>
<script type='text/javascript' src='http://www.codediesel.com/wp-content/themes/sparkling/inc/js/skip-link-focus-fix.js?ver=20140222'></script>
<script type='text/javascript' src='http://www.codediesel.com/wp-content/plugins/multi-column-tag-map/mctagmap.js?mctm_ver=12.0.4&#038;ver=1'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var sharing_js_options = {"lang":"en","counts":"1"};
/* ]]> */
</script>
<script type='text/javascript' src='http://www.codediesel.com/wp-content/plugins/jetpack/modules/sharedaddy/sharing.js?ver=3.4.3'></script>
<script type='text/javascript' src='http://www.codediesel.com/wp-content/plugins/akismet/_inc/form.js?ver=3.0.4'></script>
<link rel='stylesheet' id='jetpack_css-css'  href='http://www.codediesel.com/wp-content/plugins/jetpack/css/jetpack.css?ver=3.4.3' type='text/css' media='all' />
<script type='text/javascript' src='http://stats.wp.com/e-201536.js' async defer></script>
<script type='text/javascript'>
	_stq = window._stq || [];
	_stq.push([ 'view', {v:'ext',j:'1:3.4.3',blog:'87781247',post:'3491',tz:'5.5'} ]);
	_stq.push([ 'clickTrackerInit', '87781247', '3491' ]);
</script>

</body>
</html>
<!-- Dynamic page generated in 1.612 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2015-09-01 04:40:31 -->

<!-- super cache -->